package org.dxy.app.config;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.eventbus.EventBus;
import org.dxy.domain.event.DomainEvent;
import org.dxy.trigger.listener.EventListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Configuration
public class GuavaConfig {

    @Bean(name = "cache")
    public Cache<String, String> cache() {
        return CacheBuilder.newBuilder()
                .expireAfterWrite(3, TimeUnit.SECONDS)
                .build();
    }


    @Bean
    public EventBus eventBus(List<EventListener<? extends DomainEvent>> allListeners) {
        EventBus eventBus = new EventBus();
        allListeners.forEach(eventBus::register);
        return eventBus;
    }
}
